// pages/productList/productList.js
import * as API_Store from '../../api/store'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productList:[],
    finished:false,
    params: {
      //经度
      longitude:'',
      //纬度
      latitude:'',
      cityCode:'',
      newUseFlag:'',
      pageNum:1,
      pageSize:10,
      secondClassifyId:null,
      searchValue:''
    },
    starList:[,,,,,],

  },
  toDetail (e) {
    let id = e.currentTarget.dataset.id
    getApp().globalData.selectStoreId = id
    wx.navigateTo({
      url: '/pages/storeHome/storeHome?id=' + id,
    })
  },
  flushList(){
    this.getList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    console.log(options)
    let globalData=getApp().globalData;
    let statusBarHeight=globalData.systemInfo.statusBarHeight;
    if (!globalData.latitude && !globalData.longitude) {
      await getApp().refreshLocation()
    }
    let lat = globalData.latitude
    let lng = globalData.longitude
    let cityCode = globalData.cityCode
    this.setData({
      statusBarHeight:statusBarHeight,
      'params.latitude':lat,
      'params.longitude':lng,
      'params.secondClassifyId':options.classifyId || '',
      'params.searchValue':options.searchKey || '',
      'params.cityCode':cityCode,
      secondClassifyId:options.classifyId
    })
    this.getList()
  },
  goStore(e){
    wx.navigateTo({
      url: '/pages/storeHome/storeHome?id='+e.currentTarget.dataset.id,
    })
  },
  input (e) {
    let val = e.detail.value
    this.setData({
      'params.searchValue': val
    })
  },
  search () {
    this.data.params.pageNum = 1
    this.data.productList = []
    this.getList()
  },
  getList(){
    API_Store.getList(this.data.params).then(res=>{
      let arr = this.data.productList
      for(let item of res.data.rows){
        item.storeLevel = item.storeLevel.toFixed(1)
      }
      arr.push(...res.data.rows)
      this.setData({
        finished:res.data.lastPage,
        productList:arr
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.finished){
      return
    }
    var page = this.data.params.pageNum + 1

    this.data.params.pageNum = page
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})